﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class EShop_ManageEmailMarketing : System.Web.UI.Page
{
    ManageEmailMarketing mem = new ManageEmailMarketing();
    UsersRepository URepo = new UsersRepository();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack) return;
        GetEmailInfoHasBought();
         GetEmailTemplateInfo();
    }
    void GetEmailInfoHasBought()
    {
        try
        {
            HttpCookie _user = Request.Cookies["userinfo"];
            int UserId = URepo.GetUserIdByName(_user["username"]);
            List<IEmailMarketings> list = mem.GetAllEmailPackageBought(UserId);
            if (list.Count > 0)
            {
                grvManageEmailMarketing.DataSource = list;
                grvManageEmailMarketing.DataBind();
            }
            else
            {
                lblEmailMarketingAlert.Text = "Bạn chưa mua gói email.";
                lblEmailMarketingAlert.Visible = true;
            }
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
    }
    public string GetEmailPackageStatus(string status)
    {
        string str = string.Empty;
        switch (int.Parse(status))
        {
            case 1: str = "Active"; break;
            case 2: str = "Disable"; break;
            case 3: str = "Unactive"; break;
        }
        return str;
    }
    void GetEmailTemplateInfo()
    {
        try
        {
            HttpCookie _user = Request.Cookies["userinfo"];
            int UserId = URepo.GetUserIdByName(_user["username"]);
            ddlEmailTemplate.DataSource = mem.GetEmailPackageToSendMail(UserId);
            ddlEmailTemplate.DataTextField = "EmailPackageName";
            ddlEmailTemplate.DataValueField = "BuyEmailDetailId";
            ddlEmailTemplate.DataBind();
            ddlEmailTemplate.Items.Insert(0, new ListItem("", "-1"));
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
    }

    protected void btnSendMail_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtSenderId.Text.Substring(txtSenderId.Text.Length - 1, 1).Equals(";"))
                txtSenderId.Text = txtSenderId.Text.Substring(0, txtSenderId.Text.Length - 1);
            string[] arrSenderName = txtSenderId.Text.Split(new char[] { ';' });
            HttpCookie _user = Request.Cookies["userinfo"];
             int UserId = URepo.GetUserIdByName(_user["username"]);
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                for (int i = 0; i < arrSenderName.Count(); i++)
                {
                    UsersRepository _UsersRepository = new UsersRepository();
                    int SenderId = _UsersRepository.GetIdByEmailOrUsername(arrSenderName[i]);
                    string EmailContent = txtEmailContent.Text.Replace("$username", _user["username"]).Replace("$sender", arrSenderName[i]);
                    mem.SendMail(SenderId, txtEmailTitle.Text, int.Parse(ddlEmailTemplate.SelectedValue), EmailContent, UserId);
                }
                lblMessage.Visible = true;
                ddlEmailTemplate.SelectedValue = "-1";
                txtEmailTitle.Text = string.Empty;
                txtSenderId.Text = string.Empty;
                txtEmailContent.Text = string.Empty;
                hdMailRemaind.Value = "0";
            }
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
    }
    protected void ddlEmailTemplate_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (ddlEmailTemplate.SelectedValue != "-1")
            {
                HttpCookie _user = Request.Cookies["userinfo"];
                using (Up1VNDataContext dc = new Up1VNDataContext())
                {
                    int amount_email_sended = 0;
                    int point = 0;
                    int point_minus = 0;
                    amount_email_sended = mem.GetTotalMailSender(int.Parse(ddlEmailTemplate.SelectedValue));
                    List<IEmailMarketings> list = mem.GetPointAndPointMinus(int.Parse(ddlEmailTemplate.SelectedValue));
                    if (list.Count() > 0)
                    {
                        point = int.Parse(list[0].Point.ToString());
                        point_minus = int.Parse(list[0].PointMinus.ToString());
                    }
                    int a = (point - amount_email_sended * point_minus) / point_minus;
                    if (a > 0)
                        lblAlertRemaind.Text = string.Format("(Còn gởi được {0} email.)", a);
                    else
                        lblAlertRemaind.Text = string.Format("(Gói này đã sử dụng hết email.)", a);
                    lblAlertRemaind.Visible = true;
                    txtEmailContent.Text = mem.GetEmailContentById(int.Parse(ddlEmailTemplate.SelectedValue));
                    hdMailRemaind.Value = a.ToString();
                }
            }
            else
            {
                txtEmailContent.Text = string.Empty;
                txtEmailTitle.Text = string.Empty;
                txtSenderId.Text = string.Empty;
                lblAlertRemaind.Text = string.Empty;
            }
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
    }
}